/*
 * Copyright (C), 2002-2015, 苏宁易购电子商务有限公司
 * FileName: WorkOrderAction.java
 * Author:   admin
 * Date:     2015年12月22日 下午2:45:05
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.app.snpaasadmin.action;

import com.opensymphony.xwork2.ActionSupport;
import com.suning.app.snpaasadmin.domain.WorkOrderPageResult;
import com.suning.app.snpaasadmin.domain.WorkOrderResult;
import com.suning.app.snpaasadmin.operation.WorkOrderDBOperation;
import com.suning.app.snpaasadmin.pojo.CfWorkOrderExternal;
import com.suning.app.snpaasadmin.util.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 工单管理<br>
 * 〈功能详细描述〉
 *
 * @author admin
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Controller
public class WorkOrderAction extends ActionSupport {

    private static final long serialVersionUID = 1L;
    
    private Map<String,Object> dataMap;
    
    private String content;
    
    private Integer workOrderId;
    
    private Integer serialNum;
    
    private Integer type;
    
    private Integer page;
    
    private String context;
    
    @Autowired
    private WorkOrderDBOperation workOrderdb;

    
    /**
     * 
     * 功能描述: <br>
     * 全部工单
     *
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getListByParam() throws Exception {
        dataMap = new HashMap<String, Object>();
        List<WorkOrderResult> workOrderRes = new ArrayList<WorkOrderResult>();
        WorkOrderPageResult workOrderResPage = new WorkOrderPageResult();
        int pages = 0;
        if(type == Constants.WORKORDER_TYPE_ALL) {
            List<WorkOrderResult> workOrders = workOrderdb.getListReadCfWorkorder();
            
            //判断总共页数
            pages = workOrders.size()/Constants.PAGE_SIZE + ((workOrders.size() % Constants.PAGE_SIZE == 0) ? 0 : 1);
            if(page > 0 && page < pages) {
                workOrderRes.addAll(workOrders.subList(page*Constants.PAGE_SIZE+1, (page+1*Constants.PAGE_SIZE)));
            }
            if( page == pages ) {
                workOrderRes.addAll(workOrders);
            }
        }
        if(type == Constants.WORKORDER_TYPE_UNREAD) {
            List<WorkOrderResult> workOrders = workOrderdb.getListUnreadCfWorkorder();
            pages = workOrders.size()/Constants.PAGE_SIZE + ((workOrders.size() % Constants.PAGE_SIZE == 0) ? 0 : 1);
            if(page > 0 && page < pages) {
                workOrderRes.addAll(workOrders.subList(page*Constants.PAGE_SIZE+1, (page+1*Constants.PAGE_SIZE)));
            }
            if( page == pages ) {
                workOrderRes.addAll(workOrders);
            }
        }
        
        //根据order_state判断  order_state=0 处理中
        if(type == Constants.WORKORDER_TYPE_HANDLEING) {
            List<WorkOrderResult> workOrders = workOrderdb.getListHandlingCfWorkorder();
            pages = workOrders.size()/Constants.PAGE_SIZE + ((workOrders.size() % Constants.PAGE_SIZE == 0) ? 0 : 1);
            if(page > 0 && page < pages) {
                workOrderRes.addAll(workOrders.subList(page*Constants.PAGE_SIZE+1, (page+1*Constants.PAGE_SIZE)));
            }
            if( page == pages ) {
                workOrderRes.addAll(workOrders);
            }
        }
        
        //order_state=1 关闭
        if(type == Constants.WORKORDER_TYPE_CLOSE) {
            List<WorkOrderResult> workOrders = workOrderdb.getListCloseCfWorkorder();
            pages = workOrders.size()/Constants.PAGE_SIZE + ((workOrders.size() % Constants.PAGE_SIZE == 0) ? 0 : 1);
            if(page > 0 && page < pages) {
                workOrderRes.addAll(workOrders.subList(page*Constants.PAGE_SIZE+1, (page+1*Constants.PAGE_SIZE)));
            }
            if( page == pages ) {
                workOrderRes.addAll(workOrders);
            }
        }
        workOrderResPage.setWorkorderList(workOrderRes);
        workOrderResPage.setPage(pages);
        workOrderResPage.setPageNow(page);
        dataMap.put("data", workOrderResPage);
        dataMap.put("success", true);
        return SUCCESS;
    }
    
    /**
     * 
     * 功能描述: <br>
     * 未读工单
     *
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String listUnread() throws Exception {
        dataMap = new HashMap<String, Object>(); 
        List<WorkOrderResult> Unreadworkorders = workOrderdb.getListUnreadCfWorkorder();
        dataMap.put("data", Unreadworkorders);
        dataMap.put("success", true);
        return SUCCESS;
    }
    
    /**
     * 
     * 功能描述: <br>
     *  获取每一条工单
     *
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String getDetail() throws Exception {
        dataMap = new HashMap<String, Object>();
        List<CfWorkOrderExternal> data = workOrderdb.getWorkOrderDetail(workOrderId);
        dataMap.put("data", data);
        dataMap.put("success", true);
        return SUCCESS;
    }
    
    /**
     * 
     * 功能描述: <br>
     *  运维人员回复用户
     *  使用post方法请求
     * @return
     * @throws Exception
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public String reply() throws Exception {
        dataMap = new HashMap<String, Object>();
        Boolean result = workOrderdb.replyUser(context, workOrderId, serialNum);
        dataMap.put("success", result);
        return SUCCESS;
    }
    
    public String closeWorkOrder() throws Exception {
        
        //将orderState置成1
        workOrderdb.closeWorkOrder(workOrderId);
        return SUCCESS;
    }
    
    /**
     * @return the dataMap
     */
    public Map<String, Object> getDataMap() {  
        return dataMap;  
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * @return the workOrderId
     */
    public Integer getWorkOrderId() {
        return workOrderId;
    }

    /**
     * @param workOrderId the workOrderId to set
     */
    public void setWorkOrderId(Integer workOrderId) {
        this.workOrderId = workOrderId;
    }

    /**
     * @return the serialNum
     */
    public Integer getSerialNum() {
        return serialNum;
    }

    /**
     * @param serialNum the serialNum to set
     */
    public void setSerialNum(Integer serialNum) {
        this.serialNum = serialNum;
    }

    /**
     * @return the type
     */
    public Integer getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(Integer type) {
        this.type = type;
    }

    /**
     * @return the page
     */
    public Integer getPage() {
        return page;
    }

    /**
     * @param page the page to set
     */
    public void setPage(Integer page) {
        this.page = page;
    }

    /**
     * @return the context
     */
    public String getContext() {
        return context;
    }

    /**
     * @param context the context to set
     */
    public void setContext(String context) {
        this.context = context;
    }  
}
